package com.ht.demo.service.manager.system.impl;

import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.constans.SystemConstant;
import com.ht.demo.common.pojo.bo.system.SysMenuBO;
import com.ht.demo.dao.dao.system.SysRoleDao;
import com.ht.demo.dao.dao.system.SysRoleMenuDao;
import com.ht.demo.dao.domain.system.SysRoleDO;
import com.ht.demo.dao.domain.system.SysRoleMenuDO;
import com.ht.demo.service.manager.system.SysMenuManager;
import com.ht.demo.service.manager.system.SysRoleMenuManager;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Component
public class SysRoleMenuManagerImpl implements SysRoleMenuManager {
    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private SysRoleMenuDao sysRoleMenuDao;
    @Autowired
    private SysMenuManager menuManager;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean saveRoleMenuRelation(Long sysRoleId, List<Long> menuIdList) {
        if (sysRoleId == null) {
            return false;
        }
        Integer i = sysRoleMenuDao.deleteByRoleId(sysRoleId);

        if (CollectionUtils.isEmpty(menuIdList)) {
            return true;
        }
        List<SysMenuBO> menus = menuManager.findMenuByIdList(menuIdList);
        for (SysMenuBO menu : menus) {
            SysRoleMenuDO insert = new SysRoleMenuDO();
            insert.setSysRoleId(sysRoleId);
            insert.setSysMenuId(menu.getSysMenuId());
            insert.setMenuName(menu.getMenuName());
            insert.setPerms(menu.getPerms());
            insert.setUpdateTime(new Date());

            sysRoleMenuDao.insert(insert);
        }

        return true;
    }

    @Override
    public Boolean deleteRelationByMenuId(Long sysMenuId) {
        sysRoleMenuDao.deleteByMenuId(sysMenuId);
        return Boolean.TRUE;
    }

    @Override
    public Set<String> getPermission(Long sysRoleId) {
        Set<String> permSet = new HashSet<>();
        if (sysRoleId == null) {
            return permSet;
        }
        SysRoleDO role = sysRoleDao.findById(sysRoleId);
        if (role == null) {
            return permSet;
        }
        //超级管理员返回所有权限
        if (SystemConstant.SUPER_ADMIN.equals(role.getRoleKey())) {
            permSet.add("*:*:*");
            return permSet;
        }
//        List<Long> menuIds = sysRoleMenuDao.findMenuIdsByRoleId(sysRoleId);
//        permSet = sysMenuDao.findPermByIds(menuIds);
        permSet = sysRoleMenuDao.findPermissionByRoleId(role.getId());

        return permSet;
    }

    @Override
    public List<Long> getMenuIdListByUserId(Long sysRoleId) throws AppException {
        if (sysRoleId == null) {
            return new ArrayList<>();
        }
        List<Long> menuIds = sysRoleMenuDao.findMenuIdsByRoleId(sysRoleId);

        return menuIds;
    }
}
